package com.example.myapplication;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.google.gson.Gson;

public class LoginActivity extends AppCompatActivity {

    private TextView textView;
    private TextView textView2;
    private EditText editTextTextPersonName;
    private EditText editTextTextPersonName2;
    private CheckBox checkBox;
    private CheckBox checkBox2;
    private Button button;
    private Button button2;
    private SharedPreferences sp;

    @Override
    protected void onResume() {
        super.onResume();
        boolean isSavePwd = sp.getBoolean("isSavePwd", false);
        checkBox.setChecked(isSavePwd);
        boolean isAutoLogin = sp.getBoolean("isAutoLogin", false);
        checkBox2.setChecked(isAutoLogin);
        String userName = sp.getString("userName", "");
        String userPwd = sp.getString("userName", "");
        if (isSavePwd) {
            editTextTextPersonName.setText(userName);
            editTextTextPersonName2.setText(userPwd);
        }
        if (isAutoLogin) {
            if (!userName.equals("")&!userPwd.equals("")){
                button.performClick();
            }
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        initView();
        checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                SharedPreferences.Editor editor = sp.edit();
                editor.putBoolean("isSavePwd", isChecked);
                editor.commit();
            }
        });
        checkBox2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                SharedPreferences.Editor editor = sp.edit();
                editor.putBoolean("isSavePwd", isChecked);
                editor.putBoolean("isAutoLogin", isChecked);
                editor.commit();
            }
        });
        sp = getSharedPreferences("loginSetting", MODE_PRIVATE);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String userName = editTextTextPersonName.getText().toString().trim();
                String userPwd = editTextTextPersonName2.getText().toString().trim();
                HttpUtil.asyncRequest(new HttpUtil.AsyncCallBack() {
                    @Override
                    public void onResponse(String json) {
                        LoginBean loginBean = new Gson().fromJson(json, LoginBean.class);
                        if (loginBean.getRESULT().equals("S")) {
                            SharedPreferences.Editor editor = sp.edit();
                            if (checkBox.isChecked()) {
                                editor.putString("userName", userName);
                                editor.putString("userPwd", userPwd);
                            }else {
                                editor.putString("userName", null);
                                editor.putString("userPwd", null);
                            }
                            Intent intent = new Intent(getApplicationContext(), MainActivity.class);
                            startActivity(intent);
                            return;
                        } else if (loginBean.getRESULT().equals("F")) {
                            Toast.makeText(LoginActivity.this, "登陆失败", Toast.LENGTH_SHORT).show();
                        }
                    }

                    @Override
                    public void onFailure(Exception e) {
                        Log.i("LoginLog", e.getMessage());
                    }
                }, "user_login", new String[]{"UserName", "UserPwd"}, userName, userPwd);
            }
                });

            }


    private void initView() {
        textView = (TextView) findViewById(R.id.textView);
        textView2 = (TextView) findViewById(R.id.textView2);
        editTextTextPersonName = (EditText) findViewById(R.id.editTextTextPersonName);
        editTextTextPersonName2 = (EditText) findViewById(R.id.editTextTextPersonName2);
        checkBox = (CheckBox) findViewById(R.id.checkBox);
        checkBox2 = (CheckBox) findViewById(R.id.checkBox2);
        button = (Button) findViewById(R.id.button);
        button2 = (Button) findViewById(R.id.button2);
    }
}